package com.hustzp.com.xichuangzhu;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.widget.RadioGroup;

import com.hustzp.com.xichuangzhu.adapter.HomePagerAdapter;
import com.hustzp.com.xichuangzhu.fragment.CategoryFragment;
import com.hustzp.com.xichuangzhu.fragment.DayFragment;
import com.hustzp.com.xichuangzhu.fragment.LibraryFragment;
import com.hustzp.com.xichuangzhu.fragment.MyFragment;
import com.hustzp.com.xichuangzhu.fragment.ReViewFragment;
import com.umeng.analytics.MobclickAgent;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Locale;

public class MainActivity extends XCZBaseFragmentActivity {
    private RadioGroup rgHome;
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 简体
        if("1".equals(XichuangzhuApplication.getInstance().getFanjian())){

            Configuration config = getResources().getConfiguration();
            DisplayMetrics dm = getResources() .getDisplayMetrics();
            config.locale = Locale.SIMPLIFIED_CHINESE;
            getResources().updateConfiguration(config, dm); //强制设置语言类

        }else {

            Configuration config = getResources().getConfiguration();
            DisplayMetrics dm = getResources() .getDisplayMetrics();
            config.locale = Locale.TAIWAN;
            //强制设置语言类
            getResources().updateConfiguration(config, dm);

        }
        setContentView(R.layout.activity_main);
        rgHome = (RadioGroup) findViewById(R.id.rgHomeGroup);
        rgHome.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int index = 1;
                switch (checkedId) {
                    case R.id.radioButton1:
                        index = 0;
                        break;
                    case R.id.radioButton2:
                        index = 1;
                        break;
                    case R.id.radioButton3:
                        index = 2;
                        break;
                    case R.id.radioButton4:
                        index = 3;
                        break;
                    case R.id.radioButton5:
                        index = 4;
                        break;
                }
                //切换无动画
                mPager.setCurrentItem(index, false);
            }
        });
        initViewPager();
    }


    /*
         * 初始化ViewPager
         */
    public void initViewPager() {
        mPager = (ViewPager) findViewById(R.id.homePager);
        fragmentList = new ArrayList<>();
        Fragment oneFragment = new ReViewFragment();
        Fragment twoFragment = new DayFragment();
        Fragment threeFragment = new CategoryFragment();
        Fragment fourFragment = new LibraryFragment();
        Fragment fiveFragment = new MyFragment();
        fragmentList.add(oneFragment);
        fragmentList.add(twoFragment);
        fragmentList.add(threeFragment);
        fragmentList.add(fourFragment);
        fragmentList.add(fiveFragment);

        //给ViewPager设置适配器
        mPager.setOffscreenPageLimit(fragmentList.size());
        HomePagerAdapter homePagerAdapter = new HomePagerAdapter(getSupportFragmentManager(), fragmentList);
        mPager.setAdapter(homePagerAdapter);
        //切换无动画
        mPager.setCurrentItem(0, false);//设置当前显示标签页为第一页
    }

    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }


    public static String getDeviceInfo(Context context) {
        try {
            org.json.JSONObject json = new org.json.JSONObject();
            android.telephony.TelephonyManager tm = (android.telephony.TelephonyManager) context
                    .getSystemService(Context.TELEPHONY_SERVICE);
            String device_id = null;
//            if (checkPermission(context, permission.READ_PHONE_STATE)) {
//                device_id = tm.getDeviceId();
//            }
            String mac = null;
            FileReader fstream = null;
            try {
                fstream = new FileReader("/sys/class/net/wlan0/address");
            } catch (FileNotFoundException e) {
                fstream = new FileReader("/sys/class/net/eth0/address");
            }
            BufferedReader in = null;
            if (fstream != null) {
                try {
                    in = new BufferedReader(fstream, 1024);
                    mac = in.readLine();
                } catch (IOException e) {
                } finally {
                    if (fstream != null) {
                        try {
                            fstream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (in != null) {
                        try {
                            in.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
            json.put("mac", mac);
            if (TextUtils.isEmpty(device_id)) {
                device_id = mac;
            }
            if (TextUtils.isEmpty(device_id)) {
                device_id = android.provider.Settings.Secure.getString(context.getContentResolver(),
                        android.provider.Settings.Secure.ANDROID_ID);
            }
            json.put("device_id", device_id);
            return json.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
